import DateUtil from "../../engine/DateUtil";
import { Logger } from "../../engine/Logger";
import { SpriteFrameComponent } from "../../engine/SpriteFrameComponent";
import ViewBase from "../../engine/ViewBase";
import { HttpHelper } from "../../net/HttpHelper";
import { SocketClient } from "../../net/SocketClient";
import { GoodConfig } from "../config/GoodConfig";
import { UIHelper } from "../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewShop extends ViewBase {

    private tabIndex: number = 0;
    private btnTabList: Array<cc.Node> = []
    private scrollViewList: Array<cc.Node> = []

    public static PanelType = {
        Normal: 1,
        Ziyouchang: 2
    }

    private panelType: number = 1
    private timeNode: cc.Node;
    private lbLeftTime: cc.Label
    private endTime: number

    public onInitChild(panelType: number = 1, endTime: number = 0): void {
        this.panelType = panelType
        this.endTime = endTime + DateUtil.now() + 50 * 1000
    }

    onEnable(): void {
        super.onEnable()
        let btnClose: cc.Node = this.findChild(this.node, "btnClose")
        UIHelper.onAddEvent(btnClose, () => {
            this.closeView()
            if (this.panelType == ViewShop.PanelType.Ziyouchang) {
                SocketClient.req_ziyou_release_room_no_charge()
            }
        })

        this.timeNode = this.findChild(this.node, "timeNode")
        this.timeNode.active = false
        this.lbLeftTime = this.getChild(this.timeNode, "lbLeftTime", cc.Label)
        for (let i = 0; i < 2; i++) {
            let btnTab: cc.Node = this.findChild(this.node, "btnTab" + i)
            btnTab.active = true
            this.btnTabList[i] = btnTab
            let scrollView: cc.Node = this.findChild(this.node, "scrollView" + i)
            this.scrollViewList[i] = scrollView;
            UIHelper.onAddEvent(btnTab, () => {
                this.tabIndex = i
                this.refreshTab()
            })
        }
        if (this.panelType == ViewShop.PanelType.Ziyouchang) {
            this.tabIndex = 0
            this.btnTabList[1].active = false
            this.timeNode.active = true
        }
        this.initJinDou()
        this.initZuanshi()
        this.refreshTab()
    }

    private refreshTab() {
        for (let i = 0; i < this.btnTabList.length; i++) {
            if (i == this.tabIndex) {
                this.scrollViewList[i].active = true
                this.btnTabList[i].getChildByName("noSel").active = false
                this.btnTabList[i].getChildByName("select").active = true
            } else {
                this.scrollViewList[i].active = false
                this.btnTabList[i].getChildByName("noSel").active = true
                this.btnTabList[i].getChildByName("select").active = false
            }
        }
    }

    private initJinDou() {
        for (let i = 0; i < 6; i++) {
            let btnCharge: cc.Node = this.findChild(this.scrollViewList[0], "view/content/btnCharge" + i)
            UIHelper.onAddEvent(btnCharge, () => {
                let config: GoodConfig = GoodConfig.configMap.get(i + 7);
                Logger.log("GoodConfig===", config)
                HttpHelper.req_pay(config.id)
            }, cc.Button.Transition.COLOR)
        }
    }

    private initZuanshi() {
        Logger.log("initZuanshi==")
        for (let i = 0; i < 4; i++) {
            let btnZuanshi: cc.Node = this.findChild(this.scrollViewList[1], "view/content/btnCharge" + i)
            UIHelper.onAddEvent(btnZuanshi, () => {
                let config: GoodConfig = GoodConfig.configMap.get(i + 1);
                Logger.log("GoodConfig===", config)
                HttpHelper.req_pay(config.id)
            }, cc.Button.Transition.COLOR)
        }
    }

    protected update(dt: number): void {
        if (this.timeNode.active) {
            let leftTime: number = this.endTime - DateUtil.now()
            this.lbLeftTime.string = Math.floor(leftTime / 1000) + "秒"
            if (leftTime <= 0) {
                this.closeView()
                SocketClient.req_ziyou_release_room_no_charge()
            }
        }
    }


}
